package com.lcl.test2;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.PersistableBundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import migu.vr.fileutil.FileSaver;

public class MainActivity extends AppCompatActivity {

    public static final String FILE_NAME = "a.mp4";
    public static String LOCAL_DESTINATION_NAME = "";

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LOCAL_DESTINATION_NAME = getExternalCacheDir().getPath() +  "/a.mp4";
        Log.e("111q","LOCAL_DESTINATION_NAME" + LOCAL_DESTINATION_NAME);

        Button btn_copy = findViewById(R.id.btn_copy);
        btn_copy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InputStream inputStream = getAssetsFromLocal();
                saveInputStreamToLocal(inputStream);
            }
        });

        Button btn_start_next_page = findViewById(R.id.btn_start_next_page);
        btn_start_next_page.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, SecondActivity.class));
            }
        });
    }

    /**
     * 从asset获取文件为inputstream
     * @return
     */
    private InputStream getAssetsFromLocal() {
        try {
            InputStream inputStream = getAssets().open(FILE_NAME);
            return inputStream;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    private void saveInputStreamToLocal(InputStream inputStream) {
        try {
            writeToLocal(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
//        Executor singleThreadExecutor = Executors.newSingleThreadExecutor();
//        singleThreadExecutor.execute(new Runnable() {
//            @Override
//            public void run() {
//                try {
//                    writeToLocal(inputStream);
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        });
    }


    private void writeToLocal( InputStream input)
            throws IOException {


        int index;
        byte[] bytes = new byte[1024];
//        FileOutputStream downloadFile = new FileOutputStream(LOCAL_DESTINATION_NAME);
        FileSaver fileSaver = new FileSaver(LOCAL_DESTINATION_NAME);
        while ((index = input.read(bytes)) != -1) {
            fileSaver.write(bytes);
//            downloadFile.flush();
        }
        input.close();
        fileSaver.close();
    }

}

